package com.sample.springbatch.job.sample.writer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.MultiResourceItemWriter;
import org.springframework.batch.item.file.SimpleResourceSuffixCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Flat文件格式写入类  实现了分隔符格式
 * Created by zwg on 2017/8/8.
 */
@Component
public class RepayOrderNewMultiResourceFlatFileItemWriter<RepayOrderInfo> extends MultiResourceItemWriter {

    public static final Logger logger = LoggerFactory.getLogger(RepayOrderNewMultiResourceFlatFileItemWriter.class);

    @Autowired
    RepayOrderNewFlatFileItemWriter repayOrderNewFlatFileItemWriter;

    public RepayOrderNewMultiResourceFlatFileItemWriter() {
        //setResource(new PathResource("target/data/repay-orders_multi_0.dat"));
        //每个文件多少条记录
        setItemCountLimitPerResource(1000);
        setResourceSuffixCreator(new SimpleResourceSuffixCreator());
        setSaveState(true);

    }



    @PostConstruct
    public void init(){
        setDelegate(repayOrderNewFlatFileItemWriter);
    }


    @Override
    public void open(ExecutionContext executionContext) {
        String time = "";//DateFormatUtils.format(new Date(),"yyyyMMddHHmmss");
        String filePath = "target/data/repay-orders_multi_0_"+time+".dat";
        if(logger.isInfoEnabled()){
            logger.info("filePath:{}",filePath);
        }
        setResource(new PathResource(filePath));
        super.open(executionContext);
    }
}
